package com.ha.auto_skip;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ha.auto_skip.bean.JumpListBean;
import com.ha.auto_skip.databinding.ActivityJsonBinding;
import com.ha.auto_skip.utils.JsonUtils;
import com.ha.auto_skip.utils.SystemDialog;
import com.xinkao.skmvp.utils.Log2f;
import com.xinkao.skmvp.utils.MyToast;

import java.io.File;

public class JsonActivity extends AppCompatActivity {

    ActivityJsonBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityJsonBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initBindWidget();
        initBindListener();
        initLoadData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("配置文件");
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.json, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.copy) {
            ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData mClipData =ClipData.newPlainText("Label", binding.jsonInput.getText().toString());
            manager.setPrimaryClip(mClipData);
            MyToast.success("复制完成");
        } else if (item.getItemId() == android.R.id.home) {
            this.finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initBindWidget() {

    }

    private void initBindListener() {
        binding.clean.setOnClickListener(view -> binding.jsonInput.setText(""));
        binding.save.setOnClickListener(view -> {
            new SystemDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("您是想覆盖原有设置？还是追加新设置？")
                    .setNegativeButton("覆盖", (builder, view1) -> {
                        save(true);
                    })
                    .setPositiveButton("追加", (builder, view1) -> {
                        save(false);
                    })
                    .show();
        });
    }

    private void initLoadData() {
        Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getName().equals("time");
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }).create();
        String json = gson.toJson(App.getJumpListBean());
        binding.jsonInput.setText(JsonUtils.stringToJSON(json));
    }

    private void save(boolean cover) {
        try {
            if (cover) {
                App.getJumpListBean().getJumpList().clear();
            }
            Gson gson = new Gson();
            JumpListBean bean = gson.fromJson(binding.jsonInput.getText().toString(), JumpListBean.class);
            App.getJumpListBean().getJumpList().addAll(bean.getJumpList());
            App.getJumpListBean().saveAsJson();
            MyToast.success("操作完成");
        } catch (Exception e) {
            Log2f.e(e);
            MyToast.error("保存失败，已记录日志");
        }
    }
}